import requests
import os

# 保存图片的文件夹路径，如果不存在则创建
save_folder = "downloaded_images"
if not os.path.exists(save_folder):
    os.makedirs(save_folder)

for i in range(1, 21):
    # 构建图片链接
    url = f"https://p3-webcast.douyinpic.com/img/webcast/fansclub_new_advanced_badge_{i}_xmp.png~tplv-obj.image"
    try:
        # 从链接中获取图片文件名
        file_name = f"{i}.png"
        file_path = os.path.join(save_folder, file_name)

        # 发送HTTP请求获取图片内容
        response = requests.get(url)
        if response.status_code == 200:
            with open(file_path, 'wb') as f:
                f.write(response.content)
            print(f"成功下载图片: {file_name}")
        else:
            print(f"下载图片失败: {url}，状态码: {response.status_code}")
    except Exception as e:
        print(f"下载图片时出错: {e}")